def gtk_version():
    try:
        return Gtk.get_major_version()
    except:
        return Gtk.gtk_version[0]
    
try:
    from gi.repository import Gtk
    assert gtk_version() == 3
    try:
        from gi.repository import AppIndicator3 as AppIndicator
        app_indicator = True
    except:
        app_indicator = False
except:
    import gtk as Gtk
    assert gtk_version() == 2
    try:
        import appindicator as AppIndicator
        import pygtk
        app_indicator = True
    except:
        app_indicator = False

print("Using gtk version " + str(gtk_version()) + "; app indicator support: " + str(app_indicator))

from project import Project, File, Changeset
import os
import os.path

DIR = os.path.dirname(os.path.abspath( __file__ ))
ICONS = "icons"
UNCLEAN = "unclean"
CLEAN = "clean"


def load_icon(icon_name):
    return os.path.join(DIR, ICONS, icon_name + ".svg")

def load_ui(file_name):
    builder = Gtk.Builder()
    if gtk_version() == 3:
        path = os.path.join(DIR, "glade3", file_name)
        builder.add_from_file(path)
    elif gtk_version() == 2:
        path = os.path.join(DIR, "glade2", file_name)
        builder.add_from_file(path)
    return builder

def load_chooser(parent):
    if gtk_version() == 3:
        action = Gtk.FileChooserAction.SELECT_FOLDER
        cancel = Gtk.ResponseType.CANCEL
        ok = Gtk.ResponseType.OK
    elif gtk_version() == 2:
        action = Gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
        cancel = Gtk.RESPONSE_CANCEL
        ok = Gtk.RESPONSE_OK
    return Gtk.FileChooserDialog(
        "Select a folder containing repositories",
        parent,
        action,
        (Gtk.STOCK_CANCEL, cancel, Gtk.STOCK_OPEN, ok)), ok, cancel
        
def load_error_dialog(parent, text):
    if gtk_version() == 3:
        error = Gtk.MessageType.ERROR
        cancel = Gtk.ButtonsType.CANCEL
    elif gtk_version() == 2:
        error = Gtk.MESSAGE_ERROR
        cancel = Gtk.BUTTONS_CANCEL
    return Gtk.MessageDialog(
       parent,
       0,
       error,
       cancel,
       text)


#load correct form of tray indication
def load_tray(menu):
    if app_indicator:
        return TrayIndicator(menu)
    else:
        return TrayIcon(menu)

class Tray():
    UNCLEAN_STRING = " projects are unclean"
    CLEAN_STRING = "All projects are clean"

    def update(self, count):
        if self.count != count:
            self.count = count
            if count == 0:
                self.set_clean()
            else:
                self.set_unclean(count)

class TrayIndicator(Tray):
    GUIDE = "#"
    def __init__(self, menu):
        if gtk_version() == 3:
            self.indicator = AppIndicator.Indicator.new("Larunda", "indicator-messages", AppIndicator.IndicatorCategory.APPLICATION_STATUS)
            self.indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
        elif gtk_version() == 2:
            self.indicator = AppIndicator.Indicator("Larunda", "indicator-messages", AppIndicator.CATEGORY_APPLICATION_STATUS)
            self.indicator.set_status(AppIndicator.STATUS_ACTIVE)
        self.indicator.set_menu(menu)
        self.indicator.set_icon_theme_path(os.path.join(DIR, ICONS))
        self.count = 0
        self.set_clean()

    def set_unclean(self, count):
        self.set_tray(count, UNCLEAN)

    def set_clean(self):
        self.set_tray(0, CLEAN)

    def set_tray(self, count, icon):
        self.indicator.set_icon(icon)
        self.indicator.set_label(str(count), self.GUIDE)


class TrayIcon(Tray):
    def __init__(self, menu):
        self.menu = menu
        self.indicator = Gtk.StatusIcon();
        self.indicator.connect("popup-menu", self.right_click)
        self.indicator.connect("activate", self.left_click)
        self.count = 0
        self.set_clean()

    def set_unclean(self, count):
        self.set_tray(UNCLEAN)

    def set_clean(self):
        self.set_tray(CLEAN)

    def set_tray(self, icon):
        self.indicator.set_from_file(load_icon(icon))

    def left_click(self, widget):
        def position(menu, widget):
            return (Gtk.StatusIcon.position_menu(self.menu, widget))
        self.menu.popup(None, None, position, self.indicator, 0, Gtk.get_current_event_time())

    def right_click(self, icon, button, time):
        def position(menu, icon):
            return (Gtk.StatusIcon.position_menu(self.menu, icon))
        self.menu.popup(None, None, position, self.indicator, button, time)
